home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LT_LayoutMenus.c < prev    next >
C/C++ Source or Header  |  1996-08-22  |  5KB  |  229 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. struct Menu *
  15. LT_LayoutMenus(LayoutHandle *handle,struct NewMenu *menuTemplate,...)
  16. {
  17.     struct Menu    *Result;
  18.     va_list      VarArgs;
  19.  
  20.     va_start(VarArgs,menuTemplate);
  21.     Result = LT_LayoutMenusA(handle,menuTemplate,(struct TagItem *)VarArgs);
  22.     va_end(VarArgs);
  23.  
  24.     return(Result);
  25. }
  26.  
  27.  
  28. /*****************************************************************************/
  29.  
  30.  
  31. /****** gtlayout.library/LT_LayoutMenusA ******************************************
  32. *
  33. *   NAME
  34. *    LT_LayoutMenusA -- Create a menu according to a template.
  35. *
  36. *   SYNOPSIS
  37. *    Menu = LT_LayoutMenusA(Handle,Template,Tags);
  38. *     D0                      A0     A1      A2
  39. *
  40. *    struct Menu *LT_LayoutMenusA(LayoutHandle *,struct NewMenu *,
  41. *
  42. *        struct TagItem *);
  43. *
  44. *    Menu = LT_LayoutMenus(Handle,Template,...);
  45. *
  46. *    struct Menu *LT_LayoutMenus(LayoutHandle *,struct NewMenu *,...);
  47. *
  48. *   FUNCTION
  49. *    Unlike the corresponding routines in gadtools.library
  50. *    LT_LayoutMenusA() will both create and layout a menu.
  51. *    Also included is locale support.
  52. *
  53. *   INPUTS
  54. *    Handle - Pointer to LayoutHandle structure.
  55. *
  56. *    Template - Address of a ready-to-use NewMenu table to
  57. *        create the menu from.
  58. *
  59. *    Tags - Tagitem list to control menu attributes
  60. *
  61. *    Tags:
  62. *
  63. *    LAMN_FirstLabel (LONG) - Locale ID of the first string to
  64. *        use as a menu title/item/subitem label. This tag
  65. *        works in conjunction with LA_LastLabel.
  66. *
  67. *    LAMN_LastLabel (LONG) - Locale ID of the last string to
  68. *        use as a menu title/item/subitem label. This tag
  69. *        works in conjunction with LA_FirstLabel. The code
  70. *        will loop through FirstLabel..LastLabel and assign
  71. *        the corresponding locale text for each ID to the
  72. *        NewMenu.nm_Label entries. Labels which are already
  73. *        initialized with NM_BARLABEL are skipped.
  74. *
  75. *    LAMN_LabelTable (LONG *) - Pointer to an array of IDs
  76. *        to use for building the menu labels. This requires
  77. *        that a locale hook is provided with the layout handle.
  78. *        The array is terminated by -1.
  79. *
  80. *   RESULT
  81. *    Menu - Pointer to a Menu structure. You can free this
  82. *           using gadtools.library/FreeMenus().
  83. *
  84. ******************************************************************************
  85. *
  86. */
  87.  
  88. struct Menu * LIBENT
  89. LT_LayoutMenusA(REG(a0) LayoutHandle *handle,REG(a1) struct NewMenu *menuTemplate,REG(a2) struct TagItem *TagParams)
  90. {
  91.     struct Menu *menu;
  92.  
  93.     menu = NULL;
  94.  
  95.     if(handle)
  96.     {
  97.         struct TagItem    *tag,*List = TagParams;
  98.         LONG             label,last;
  99.         LONG            *Table = NULL;
  100.         ULONG             allocSize;
  101.         BOOL              foundFirst;
  102.         BOOL              foundLast;
  103.  
  104.         foundFirst    = FALSE;
  105.         foundLast    = FALSE;
  106.  
  107.         while(tag = NextTagItem(&List))
  108.         {
  109.             switch(tag->ti_Tag)
  110.             {
  111.                 case LA_LabelTable:
  112.  
  113.                     Table = (LONG *)tag->ti_Data;
  114.                     break;
  115.  
  116.                 case LA_FirstLabel:
  117.  
  118.                     foundFirst = TRUE;
  119.  
  120.                     label = tag->ti_Data;
  121.                     break;
  122.  
  123.                 case LA_LastLabel:
  124.  
  125.                     foundLast = TRUE;
  126.  
  127.                     last = tag->ti_Data;
  128.                     break;
  129.             }
  130.         }
  131.  
  132.         if(foundFirst == foundLast || Table)
  133.         {
  134.             struct NewMenu *localTemplate = NULL;
  135.  
  136.             if(Table)
  137.             {
  138.                 foundFirst = FALSE;
  139.  
  140.                 if(handle->LocaleHook)
  141.                 {
  142.                     LONG count;
  143.  
  144.                     count = 0;
  145.  
  146.                     while(menuTemplate[count].nm_Type != NM_END)
  147.                         count++;
  148.  
  149.                     count++;
  150.  
  151.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  152.                     {
  153.                         LONG i;
  154.  
  155.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  156.  
  157.                         for(i = 0 ; *Table != -1 && i < count ; i++)
  158.                         {
  159.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  160.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)(*Table++));
  161.                         }
  162.                     }
  163.                 }
  164.  
  165.                 menuTemplate = localTemplate;
  166.             }
  167.  
  168.             if(foundFirst)
  169.             {
  170.                 if(handle->LocaleHook)
  171.                 {
  172.                     LONG count;
  173.  
  174.                     count = 0;
  175.  
  176.                     while(menuTemplate[count].nm_Type != NM_END)
  177.                         count++;
  178.  
  179.                     count++;
  180.  
  181.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  182.                     {
  183.                         LONG i;
  184.  
  185.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  186.  
  187.                         if(foundLast)
  188.                             count = last - label + 1;
  189.  
  190.                         for(i = 0 ; i < count ; i++)
  191.                         {
  192.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  193.                             {
  194.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)label);
  195.  
  196.                                 label++;
  197.                             }
  198.                         }
  199.                     }
  200.                 }
  201.  
  202.                 menuTemplate = localTemplate;
  203.             }
  204.  
  205.             if(menuTemplate)
  206.             {
  207.                 if(menu = CreateMenusA(menuTemplate,NULL))
  208.                 {
  209.                     if(!LayoutMenus(menu,handle->VisualInfo,
  210.                         GTMN_NewLookMenus,    TRUE,
  211.  
  212.                         handle->AmigaGlyph ? GTMN_AmigaKey : TAG_IGNORE,handle->AmigaGlyph,
  213.                         handle->CheckGlyph ? GTMN_Checkmark : TAG_IGNORE,handle->CheckGlyph,
  214.                     TAG_DONE))
  215.                     {
  216.                         FreeMenus(menu);
  217.  
  218.                         menu = NULL;
  219.                     }
  220.                 }
  221.             }
  222.  
  223.             LTP_Free(handle,localTemplate,allocSize);
  224.         }
  225.     }
  226.  
  227.     return(menu);
  228. }
  229.